home *** CD-ROM | disk | FTP | other *** search
- // STRecognizer.cpp : Implementation of CSTRecognizer
- #include "stdafx.h"
- #include "SimpleTerm2.h"
- #include "STRecognizer.h"
- #include "common.h"
-
- /////////////////////////////////////////////////////////////////////////////
- // CSTRecognizer
-
- // 'TERM' is a data structure that will associate the
- // coffee flavor (strValue) with it's product ID (strProductID).
- typedef struct _term
- {
- WCHAR *strValue;
- WCHAR *strProductID;
- } TERM;
-
- // Build and populate the array with a list of strings of terms to be
- // recognized, linked to their corresponding ProductID. Declare these terms
- // in all lowercase to allow case-insensitive searches to be performed.
- TERM arrTerm[] = {
- {L"cappuccino", L"CAP"},
- {L"caramel macchiato", L"CAR"},
- {L"mocha", L"MOC"},
- {L"americano", L"AME"},
- {L"breve", L"BRE"},
- {L"white chocolate mocha", L"WCM"},
- {L"espresso con panna", L"ECP"},
- {L"coffee shake", L"CS"},
- {L"iced caffe mocha", L"ICM"},
- {L"iced caffe latte", L"ICL"}
- };
-
- // cTerms is the size of the arrTerm array.
- // Alternately you can also #define cTerms (sizeof(arrTerm)/sizeof(TERM))
- const int cTerms = 10;
-
- //A helper function for string allocation methods that other functions
- //could share since the method within these functions are similar.
- HRESULT HrAllocString(BSTR *pbstr, WCHAR *sz)
- {
- if (pbstr == NULL)
- return E_POINTER; // Invalid pointer.
-
- *pbstr = SysAllocString(sz);
- if (*pbstr == NULL)
- return E_OUTOFMEMORY; // The function failed to allocate the
- // necessary memory.
-
- return S_OK; // Successful operation.
- }
-
- // Create a unique identifier for this recognizer
- // that corresponds to the ProgID of this dll.
- STDMETHODIMP CSTRecognizer::get_ProgId (BSTR * ProgId)
- {
- return HrAllocString(ProgId, L"SimpleTerm2.STRecognizer");
- }
-
- // Create a check box caption that will be
- // displayed in the Tools>Autocorrect Option>Smart Tags
- // dialog box in both Word and Excel.
- STDMETHODIMP CSTRecognizer::get_Name (INT LocaleID, BSTR * Name)
- {
- return HrAllocString(Name, L"My Simple Term2 Recognizer");
- }
-
- // Create a short phrase that describes this recognizer.
- STDMETHODIMP CSTRecognizer::get_Desc (INT LocaleID, BSTR * Desc)
- {
- return HrAllocString(Desc, L"Simple Term2 Recognizer recognizes Fourth Coffee2 flavors in documents");
- }
-
- // Register the number of smart tag types this recognizer supports.
- // 1 in this case.
- STDMETHODIMP CSTRecognizer::get_SmartTagCount (INT * Count)
- {
- if (Count == NULL)
- return E_POINTER;
-
- *Count = 1;
- return S_OK;
- }
-
- // Provide the name of the smart tag type to be supported.
- // Smart Tag type names are always in the format of namespaceURI#tagname.
- STDMETHODIMP CSTRecognizer::get_SmartTagName (INT SmartTagID, BSTR * Name)
- {
- // SmartTagID is an enumerated integer that corresponds to
- // SmartTagCount. The count will go from 1 to SmartTagCount.
- if (SmartTagID != 1)
- return E_INVALIDARG; // One or more arguments are invalid.
-
- // SMART_TAG_SCHEMA is #defined in common.h
- // In this example, only one smart tag type is supported.
- return HrAllocString(Name, SMART_TAG_SCHEMA);
-
- }
-
- // Since for this particular smart tag type there is no website to
- // support a smart tag download URL, none will be provided, so return null.
- STDMETHODIMP CSTRecognizer::get_SmartTagDownloadURL (INT SmartTagID, BSTR * DownloadURL)
- {
- if (DownloadURL == NULL)
- return E_POINTER;
-
- *DownloadURL = NULL;
- return S_OK;
- }
-
- // Search for strings in TERM arrTerm[] array. This will perform
- // a case-insensitive search through the passed-in string for each
- // of the terms supplied.
- STDMETHODIMP CSTRecognizer::Recognize (BSTR Text, IF_TYPE DataType, INT LocaleID, ISmartTagRecognizerSite * RecognizerSite)
- {
- int iTerm;
- WCHAR *pch, *strText;
- ISmartTagProperties *pSmartTag = NULL;
- BSTR bstrFac, bstrKey, bstrValue;
-
- strText = (WCHAR *)Text;
-
- // For simplicity, the check will only be until the first '\0' character.
- // Here, the string is made lowercase to check against a list of terms.
- wcslwr(strText);
-
- // Loop through the terms.
- for(iTerm = 0; iTerm < cTerms; iTerm++)
- {
- // Find each occurrence of arrTerm[iTerm] within the given Text.
- for (pch = strText; (pch = wcsstr(pch, arrTerm[iTerm].strValue)) != NULL; pch++)
- {
- bstrFac = SysAllocString(SMART_TAG_SCHEMA);
- if (bstrFac != NULL)
- {
- // Create a new property bag for the smart tag.
- RecognizerSite->GetNewPropertyBag(&pSmartTag);
- if (pSmartTag != NULL)
- {
- bstrKey = SysAllocString(PRODUCT_ID);
- bstrValue = SysAllocString(arrTerm[iTerm].strProductID);
- // Populate the property bag by writing a key into the
- // property bag. If the key name doesn't exist in the
- // property bag a new property is created.
- if (bstrKey != NULL && bstrValue != NULL)
- pSmartTag->Write(bstrKey, bstrValue);
- SysFreeString(bstrKey);
- SysFreeString(bstrValue);
- }
-
- // The smart tag will still be committed even if the
- // property bag could not be allocated or filled.
- RecognizerSite->CommitSmartTag(bstrFac, pch - strText + 1, wcslen(arrTerm[iTerm].strValue), pSmartTag);
-
- if (pSmartTag != NULL)
- pSmartTag->Release();
-
- SysFreeString(bstrFac);
- }
- }
- }
-
- return S_OK;
- }
-